#include "ILogger.h"
#include "MessageRouter.h"
#include "WString.h"
#include "IService.h"
MessageRouter::MessageRouter(ILogger& logger) : logger_(logger){
}
void MessageRouter::addService(IService & service){
	for(int i = 0; i < services_.size(); ++i){
		if(services_[i]==&service)
			return;
	}
	services_.push_back(&service);
}
void MessageRouter::postMessage(const IService& sender, const String& msg){
	logger_.log(msg);
	for(int i = 0; i < services_.size(); ++i){
		if(services_[i]!=&sender)
			services_[i]->onMessage(msg);
	}
}
void MessageRouter::broadcastMessage(const String& msg){
	logger_.log(msg);
	for(int i = 0; i < services_.size(); ++i){
		services_[i]->onMessage(msg);
	}
}
void MessageRouter::tick(long milliseconds){
	for(int i = 0; i < services_.size(); ++i){
		services_[i]->tick(milliseconds);
	}
}
IService* MessageRouter::removeService(const String& name){
	for(int i = 0; i < services_.size(); ++i){
		if(name == services_[i]->getName()){
			return services_.remove(i);
		}
	}
	return 0;
}
int MessageRouter::getServicesSize() const{
	return services_.size();

}
const IService* MessageRouter::getService(int pos) const{
	return services_[pos];
}